import Vue from 'vue'
import VueRouter from 'vue-router'

// 导入组件
// import Layout from '@/views/layout'
// import Login from '@/views/login'
// import NotFound from '@/views/404'

// 导入二级路由组件
// import Home from '@/views/layout/home'
// import QA from '@/views/layout/QA'
// import Video from '@/views/layout/video'
// import User from '@/views/layout/user'
// import Edit from '@/views/layout/user/edit'
import store from '@/store'

// 导入search组件
// import Search from '@/views/search'
// import SearchResults from '@/views/search/result'
// import Article from '@/views/article'
// import Chat from '@/views/layout/user/chat.vue'

const Layout = () => import('@/views/layout')
const Login = () => import('@/views/login')
const NotFound = () => import('@/views/404')

const Home = () => import('@/views/layout/home')
const QA = () => import('@/views/layout/QA')
const Video = () => import('@/views/layout/video')

const User = () => import(/* webpackChunkName: "user" */ '@/views/layout/user')
const Edit = () =>
  import(/* webpackChunkName: "user" */ '@/views/layout/user/edit')
const Chat = () =>
  import(/* webpackChunkName: "user" */ '@/views/layout/user/chat.vue')

const Search = () => import(/* webpackChunkName: "search" */ '@/views/search')
const SearchResults = () =>
  import(/* webpackChunkName: "search" */ '@/views/search/result')
const Article = () => import('@/views/article')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: 'home',
        component: Home,
        meta: {
          isKeepAlive: true
        }
      },
      {
        path: 'QA',
        component: QA
      },
      {
        path: 'video',
        component: Video
      },
      {
        path: 'user',
        component: User
      }
    ]
  },
  {
    path: '/search',
    component: Search
  },
  {
    path: '/article/:id',
    component: Article
  },
  {
    path: '/search/result',
    component: SearchResults
  },
  {
    path: '/user/edit',
    component: Edit
  },
  {
    path: '/user/chat',
    component: Chat
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '*',
    component: NotFound
  }
]

const router = new VueRouter({
  routes
})

// 全局的导航守卫 ==> 目标（用户没有登陆，就不能访问设定需要登录后才能访问的页面）
// 需要拦截的页面(需要登录才可以访问到)
const loginURL = ['/user', '/qa', '/video', '/user/edit']
router.beforeEach((to, from, next) => {
  const token = store.state.user.token_obj.token
  // if (token) {
  //   // 有token
  //   next()
  // } else {
  // 没有token
  // 去向地址有符合需要登陆页面就执行
  // if (loginURL.some(item => to.path === item)) {
  //   // next('/login')
  //   router.push('/login')
  // } else {
  //   next()
  // }
  // loginURL.some((item) => to.path === item) ? router.push('/login') : next()
  if (token) {
    next()
  } else {
    // 没有token
    if (loginURL.some((item) => to.path === item)) {
      // if成立，需要登录
      // next('/login') // 提示，导航跳转到/user 失败了，跳转到了/login
      // router.push('/login')
      // 带上回来的地址
      router.push({
        path: '/login',
        query: {
          back: to.fullPath
        }
      })
    } else {
      next()
    }
  }
})

export default router
